﻿using T4UtilityBelt.Writers;

namespace T4UtilityBelt.UxDesigns.Controls.Renderers.Windows
{
    public class ListBoxRenderer : ControlRenderer<ListBoxControl, IListBoxRenderer>, IListBoxRenderer
    {
        protected override void OnRenderUiXmlBegin(ListBoxControl control, CodeWriter writer)
        {
            writer.WriteBeginTag("ListBox");

            RenderControlAttributes(control, writer);

            writer.WriteAttribute("x:Name", control.Name);
            writer.AddBinding("ItemsSource", control.ItemsSource);

            writer.WriteAttribute("HorizontalContentAlignment", "Stretch");
            writer.WriteEnd();
            // todo: the binding should be rendered completely
//            if (control.ItemSelectedCommand != null)
//            {
//                writer.Write(string.Format(@"
//            <interactivity:Interaction.Triggers>
//                <interactivity:EventTrigger EventName=""SelectionChanged"">
//
//                    <commandbinding:MvxEventToCommand Command=""{{Binding {0}}}"" 
//                                 CommandParameter=""{{Binding SelectedItem, ElementName={1}}}"" />
//                </interactivity:EventTrigger>
//            </interactivity:Interaction.Triggers>
//                         ", control.ItemSelectedCommand.SourcePropertyPath, control.Name));
//            }
        }

        protected override void OnRenderUiXmlContent(ListBoxControl control, CodeWriter writer)
        {
            if (control.Templates.Count > 0)
            {
                writer.WriteFullTag("ListBox.ItemTemplate");
                foreach (ItemTemplateControl template in control.Templates)
                {
                    template.RenderUiXml(writer);
                }
                writer.WriteEndTag("ListBox.ItemTemplate");
            }
        }

        protected override void OnRenderUiXmlEnd(ListBoxControl control, CodeWriter writer)
        {
            writer.WriteEndTag("ListBox");
        }
    }
}